package com.greate.community.service;

import com.greate.community.dao.CommentMapper;
import com.greate.community.entity.Comment;
import com.greate.community.constant.CommunityConstant;
import com.greate.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * 评论相关
 */
@Service
public class CommentService implements CommunityConstant {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private DiscussPostService discussPostService;

    /**
     * 根据 id 查询评论
     *
     * @param id
     * @return
     */
    public Comment findCommentById(int id) {
        return commentMapper.selectCommentById(id);
    }

    /**
     * 根据 帖子id 对评论进行分页查询
     *
     * @param postId
     * @param offset
     * @param limit
     * @return
     */
    public List<Comment> findCommentByPost(int postId, int offset, int limit) {
        return commentMapper.selectCommentByPostId(postId, offset, limit);
    }

    public List<Comment> findReplyByComment(int rootId, int offset, int limit) {
        return commentMapper.selectCommentByRootId(rootId, offset, limit);
    }

    /**
     * 查询帖子下评论、回复的数量
     *
     * @param postId
     * @return
     */
    public int findCommentAndReplyCommentCountByPostId(int postId) {
        return commentMapper.selectCommentAndReplyCountByPostId(postId);
    }

    /**
     * 查询帖子下评论的数量
     *
     * @param postId
     * @return
     */
    public int findCommentCountByPostId(int postId) {
        return commentMapper.selectCommentCountByPostId(postId);
    }

    /**
     * 查询回复的数量
     *
     * @param rootId
     * @return
     */
    public int findReplyCount(int rootId) {
        return commentMapper.selectCountByRootId(rootId);
    }

    /**
     * 分页查询某个用户的评论/回复列表
     *
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    public List<Comment> findCommentByUserId(int userId, int offset, int limit) {
        return commentMapper.selectCommentByUserId(userId, offset, limit);
    }

    /**
     * 查询某个用户的评论/回复数量
     *
     * @param userId
     * @return
     */
    public int findCommentCountByUserId(int userId) {
        return commentMapper.selectCommentCountByUserId(userId);
    }

    /**
     * 添加评论（需要事务管理）
     *
     * @param comment
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        // Html 标签转义
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        // 敏感词过滤
        comment.setContent(sensitiveFilter.filter(comment.getContent()).getFilterText());

        // 添加评论
        int rows = commentMapper.insertComment(comment);
        if (rows == 1) {
            discussPostService.addCommentCount(comment.getPostId(), 1);
        }

        return rows;
    }

    /**
     * 删除评论
     *
     * @param comment
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int deleteComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        int rows = commentMapper.updateStatus(comment.getId(), 2);
        if (rows == 1) {
            discussPostService.addCommentCount(comment.getPostId(), -1);
        }
        return rows;
    }

    public int like(int id) {
        return commentMapper.addLikeCount(id, 1);
    }

    public int cancelLike(int id) {
        return commentMapper.addLikeCount(id, -1);
    }

    /**
     * 修改评论状态：0-正常; 1-禁用; 2-删除;
     *
     * @param id
     * @param status
     * @return
     */
    public int updateStatus(int id, int status) {
        return commentMapper.updateStatus(id, status);
    }
}
